﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using D2DEngine;
using Microsoft.Xna.Framework;
using D2DEngine.AI;

namespace D2DEngine.Components.AI
{
    public class WanderBehavior : D2DBehavior
    {
        Vector2 wanderDirection = Vector2.Zero;
        Random random = new Random();

        float changeDirectionInterval = 2.0f; // In seconds
        float timer = 0.0f;

        public WanderBehavior() : base()
        {
            
        }

        public override void OnRegister()
        {
            wanderDirection.X = (float)Math.Sin(this.sceneObject.Rotation);
            wanderDirection.Y = (float)Math.Cos(this.sceneObject.Rotation);

            base.OnRegister();
        }

        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            this.timer += elapsed;

            if (this.timer > this.changeDirectionInterval)
            {
                wanderDirection.X +=
                MathHelper.Lerp(-2.0f, 2.0f, (float)random.NextDouble());
                wanderDirection.Y +=
                    MathHelper.Lerp(-2.0f, 2.0f, (float)random.NextDouble());

                if (wanderDirection != Vector2.Zero)
                {
                    wanderDirection.Normalize();
                }                

                this.timer = 0.0f;
            }

            TurnToFace(this.sceneObject.Position + wanderDirection, .15f);
        }

    }
}
